using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UsersListPage : Page, IRequiresSessionState
{
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Select();
    }

    private void Select()
    {
        string userId = this.Session["UserId"].ToString();
        Auth runMethod = new Auth(null, this.txtUserName.Text.ToString(), null);
        try
        {
            runMethod.UpdateUserRole(this.ddlCurrentRole.SelectedValue.ToString(), this.ddlRole.SelectedValue.ToString(), Convert.ToInt16(this.txtAdvAlertDay.Text), this.cbSuperUser.Checked);
        }
        catch (Exception)
        {
        }
        this.SetTextReadOnly(true);
        this.clearText();
    }

    private void clearText()
    {
        this.txtUserName.Text = string.Empty;
        this.txtAdvAlertDay.Text = string.Empty;
        this.cbSuperUser.Checked = false;
    }

    protected void ddlRole_PreRender(object sender, EventArgs e)
    {
    }

    protected void DropDownList1_PreRender(object sender, EventArgs e)
    {
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int advAlertDays = 0;
        bool superUser = false;
        string userRoleName = "";
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            new Auth(null, e.Row.Cells[2].Text, null).GetAdvAlertDay_SU_Info(out advAlertDays, out superUser, out userRoleName);
            e.Row.Cells[3].Text = userRoleName;
            ImageButton ib = (ImageButton)e.Row.FindControl("ibDelete");
            ib.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Auth runMethod = new Auth(null, this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["UserName"].ToString(), null);
        int advAlertDays = 0;
        bool superUser = false;
        string userRoleName = "";
        runMethod.GetAdvAlertDay_SU_Info(out advAlertDays, out superUser, out userRoleName);
        this.txtUserName.Text = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["UserName"].ToString();
        this.txtAdvAlertDay.Text = advAlertDays.ToString();
        this.cbSuperUser.Checked = superUser;
        try
        {
            this.ddlRole.SelectedIndex = -1;
            this.ddlRole.Items.FindByValue(userRoleName.ToString()).Selected = true;
        }
        catch (NullReferenceException)
        {
        }
        catch (HttpException)
        {
        }
        this.SetTextReadOnly(false);
    }

    protected void ibDelete_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "UserList");
            if (!canView.CanDelete())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
                return;
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        }
        ObjectDataSource DeleteUser = new ObjectDataSource();
        try
        {
            if (!string.IsNullOrEmpty(this.GridView1.Rows[this.GridView1.SelectedIndex].Cells[2].Text.ToString()))
            {
                DeleteUser.TypeName = "IPTableAdapters.aspnet_UsersTableAdapter";
                DeleteUser.DeleteMethod = "DeleteUserByUserName";
                DeleteUser.DeleteParameters.Clear();
                DeleteUser.DeleteParameters.Add("UserName", TypeCode.String, this.GridView1.Rows[this.GridView1.SelectedIndex].Cells[2].Text.ToString());
                DeleteUser.Delete();
                this.GridView1.DataBind();
            }
        }
        catch (ArgumentOutOfRangeException)
        {
            this.lblError.Text = "Please select a record to delete.";
            this.lblError.Visible = true;
        }
        catch (Exception ex)
        {
            List<string> lstValues = new List<string>();
            //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
            this.lblError.Text = ex.Message.ToString();
            this.lblError.Visible = true;
        }
        this.clearText();
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "UserList");
            if (!canView.CanUpdate())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
                return;
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        }
        if (!IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.UserSettings + " | " + ISitemap.UserLists.ToString();
        }
        this.lblError.Visible = false;
        this.lblError.Text = string.Empty;
        this.SetTextReadOnly(true);
    }

    private void SetTextReadOnly(bool bBool)
    {
        this.ddlCurrentRole.Enabled = !bBool;
        this.ddlRole.Enabled = !bBool;
        this.txtAdvAlertDay.ReadOnly = bBool;
        this.cbSuperUser.Enabled = !bBool;
        this.lblError.Text = string.Empty;
        this.lblError.Visible = false;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Select();
    }
}
